package com.cloudclassroom.controller;

import com.cloudclassroom.entity.Admin;
import com.cloudclassroom.mapper.AdminMapper;
import com.cloudclassroom.vo.RestResult;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

/**
 * @author 92110
 */
@RestController
@RequestMapping("/admin")
@RequiredArgsConstructor
public class AdminController {
    private final AdminMapper adminMapper;
    private final PasswordEncoder encoder;

    @PostMapping("/insert")
    public RestResult<String> insertUser(@RequestBody Admin admin) {
        String uuid = UUID.randomUUID().toString().toUpperCase();
        admin.setAdminId(uuid);
        System.out.println(encoder.encode(admin.getPassword()));
        admin.setPassword(encoder.encode(admin.getPassword()));
        try {
            adminMapper.insert(admin);
            return RestResult.success("用户添加成功！");
        } catch (Exception e) {
           return RestResult.failure(500, e.getMessage());
        }
    }
    @GetMapping("/select")
    public RestResult<List<Admin>> selectUser() {
        List<Admin> userList = adminMapper.selectList(null);
        return RestResult.success(userList);
    }

    @GetMapping("/selectById/{id}")
    public RestResult<Admin> selectUserById(@PathVariable("id") String id) {
        Admin admin = adminMapper.selectById(id);
        return RestResult.success(admin);
    }
}
